/**
 * 2013 2013-5-8
 */
package com.surfilter.dp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.surfilter.dp.util.PropertyFileManager;

/**
 *
 */
public abstract class BaseDao {
	
	private Connection con = null;
	
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	
	private void initConnection(){
		Connection con = null;
		String userName = PropertyFileManager.getProp("user");
		String password = PropertyFileManager.getProp("password");
		String url = PropertyFileManager.getProp("jdbcUrl");
		try {
			con = DriverManager.getConnection(url, userName, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		this.con = con;
	}
	
	public PreparedStatement getPstmt(String sql){
		PreparedStatement pstmt = null;
		if(this.con == null){
			initConnection();
		}
		try {
			pstmt =  this.con.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pstmt;
	}
	
	
	public void closePstmt(PreparedStatement pstmt){
		try {
			pstmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void destroy(){
		if(this.con != null){
			try {
				this.con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	
}
